
package pimpserver;

class Statistics
{
    private int connections;
    private int opened_connections;
    private int broken_connections;
    private int closed_connections;
    private int max_connections;

    Statistics()
    {
        connections = 0;
        opened_connections = 0;
        broken_connections = 0;
        closed_connections = 0;
        max_connections = 0;
    }
    public void addConnection()
    {
        ++connections;
        ++opened_connections;
        if(opened_connections > max_connections)
            max_connections = opened_connections;
    }
    public void removeConnection()
    {
        if(opened_connections > 0)
        {
            --opened_connections;
            connecionClose();
        }
    }
    public void connectionBroke()
    {
        ++broken_connections;
        removeConnection();
    }
    public void connecionClose()
    {
        ++closed_connections;
    }
    public String toString()
    {
        String out = new String();
        out += "Currently opened connections: " + opened_connections + "\n";
        out += "Connections since start: " + connections + "\n";
        out += "Closed connections: " + closed_connections + "\n";
        out += "Broken connections: " + broken_connections + "\n";
        out += "Percent of failure: ";
        if(connections > 0)
            out += ((double)broken_connections/(double)connections)*100.0;
        else
            out += '0';
        out += "\nConnection peak: " + max_connections + "\n";
        return out;
    }
}